package com.xjy.rpc.config;


import com.xjy.rpc.annotation.ServerGet;
import com.xjy.rpc.netty.ThreadPool;
import com.xjy.rpc.netty.client.Client;
import com.xjy.rpc.netty.server.ServerTest;
import com.xjy.rpc.proxy.IProxy;
import com.xjy.rpc.proxy.ProxyFactory;
import com.xjy.rpc.utils.PropertiesUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @description: TODO 通过注解在redis中注册标记的接口信息,启动服务，缓存socket channel
 * @author XJY
 * @date 2024/9/29 16:01
 * @version 1.0
 */
@Configuration
@Slf4j
public class ServiceConsumerConfig implements BeanPostProcessor, EnvironmentAware , InitializingBean {

    String ip;

    {
        try {
            ip = InetAddress.getLocalHost().getHostAddress();
        } catch (UnknownHostException e) {
            ip = "127.0.0.1";
        }
    }

    RpcProperties rpcProperties;


    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {

        Class<?> aClass = bean.getClass();
        Field[] fields = aClass.getDeclaredFields();
        IProxy proxy = ProxyFactory.getProxy("CGLIB");
        if (fields != null && fields.length > 0){
            for (Field field : fields) {
                if (field.isAnnotationPresent(ServerGet.class)){
                    ServerGet serverGet = field.getAnnotation(ServerGet.class);
                    Class<?> fieldType = field.getType();
                    field.setAccessible(true);
                    //代理对象
                    Object o = null;
                    o = proxy.getProxy(fieldType);
                    try {
                        //将字段赋值
                        field.set(bean,o);
                        field.setAccessible(false);
                        log.info(beanName + " field:" + field.getName() + "注入成功");
                    } catch (IllegalAccessException e) {
                        log.info(beanName + " field:" + field.getName() + "注入失败");
                        throw new RuntimeException(e);
                    }
                }
            }
        }

        return BeanPostProcessor.super.postProcessAfterInitialization(bean, beanName);
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        Thread t = new Thread(() -> {
            Client client = new Client(ip, rpcProperties.getPort());
            client.connect();
        });
        t.setDaemon(true);
        ThreadPool.getThreadPool().execute(t);
    }

    @Override
    public void setEnvironment(Environment environment) {
        RpcProperties properties = RpcProperties.getInstance();
        PropertiesUtils.init(properties,environment);
        rpcProperties = properties;
        log.info("读取配置文件成功");
    }
}
